package errors

import (
	"fmt"

	"github.com/sirupsen/logrus"
)

const HelpUtlTitle = "在线获得帮助"

func Log(err error, msg ...string) {
	myE, ok := err.(*Error)
	var entry *logrus.Entry
	if ok {
		entry = logrus.WithField("error", myE.Error()).WithField(HelpUtlTitle, fmt.Sprintf("https://rongapi.cn/help/%d.html", myE.Code))
	} else {
		entry = logrus.WithError(err)
	}
	if len(msg) > 0 {
		entry.Errorln(msg[0])
	} else {
		entry.Errorln("执行出错")
	}
}

func Warn(code int, msg ...interface{}) {
	var printed = printErr(code, newCause(msg...))
	logrus.WithField("error", printed).WithField(HelpUtlTitle, fmt.Sprintf("https://rongapi.cn/help/%d", code)).Warnln("执行警告")
}

func Fatal(code int, msg ...interface{}) {
	var printed = printErr(code, newCause(msg...))
	logrus.WithField("error", printed).WithField(HelpUtlTitle, fmt.Sprintf("https://rongapi.cn/help/%d", code)).Fatalln("执行中断")
}
